package com.mdd.admin.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.mdd.admin.LikeAdminThreadLocal;
import com.mdd.admin.aop.Log;
import com.mdd.admin.service.ILegalCaseService;
import com.mdd.admin.validate.LegalCaseCreateValidate;
import com.mdd.admin.validate.LegalCaseSearchValidate;
import com.mdd.admin.validate.LegalCaseUpdateValidate;
import com.mdd.admin.validate.commons.IdValidate;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.LegalCaseDetailVo;
import com.mdd.admin.vo.LegalCaseListedExcelVo;
import com.mdd.admin.vo.LegalCaseListedVo;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.core.PageResult;
import com.mdd.common.exception.OperateException;
import com.mdd.common.validator.annotation.IDMust;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("api/case")
@Api(tags = "案件管理")
public class LegalCaseController {

    @Resource
    ILegalCaseService iLegalCaseService;

    @GetMapping("/list")
    @ApiOperation(value="案件列表")
    public AjaxResult<PageResult<LegalCaseListedVo>> list(@Validated PageValidate pageValidate,
                                                     @Validated LegalCaseSearchValidate searchValidate) {
        PageResult<LegalCaseListedVo> list = iLegalCaseService.list(pageValidate, searchValidate);
        return AjaxResult.success(list);
    }

    @PostMapping("/exportList")
    @ApiOperation(value="导出案件列表")
    public void exportList(@Validated @RequestBody LegalCaseSearchValidate searchValidate, HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String excelFileName = URLEncoder.encode("案件列表", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + excelFileName + ".xlsx");

            List<LegalCaseListedExcelVo> list = iLegalCaseService.list(searchValidate);
            EasyExcel.write(response.getOutputStream())
                    .head(LegalCaseListedExcelVo.class)
                    .excelType(ExcelTypeEnum.XLSX)
                    .sheet("案件列表")
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
            throw new OperateException("导出案件列表异常");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value="案件详情")
    public AjaxResult<LegalCaseDetailVo> detail(@Validated @IDMust() @RequestParam("id") Integer id) {
        LegalCaseDetailVo detail = iLegalCaseService.detail(id);
        return AjaxResult.success(detail);
    }

    @Log(title = "案件新增")
    @PostMapping("/add")
    @ApiOperation(value="案件新增")
    public AjaxResult<Object> add(@Validated @RequestBody LegalCaseCreateValidate createValidate) {
        iLegalCaseService.add(createValidate);
        return AjaxResult.success();
    }

    @Log(title = "案件编辑")
    @PostMapping("/edit")
    @ApiOperation(value="案件编辑")
    public AjaxResult<Object> edit(@Validated @RequestBody LegalCaseUpdateValidate updateValidate) {
        iLegalCaseService.edit(updateValidate);
        return AjaxResult.success();
    }

    @Log(title = "案件删除")
    @PostMapping("/del")
    @ApiOperation(value="案件删除")
    public AjaxResult<Object> del(@Validated @RequestBody IdValidate idValidate) {
        iLegalCaseService.del(idValidate.getId());
        return AjaxResult.success();
    }

    @Log(title = "切换是否展示")
    @PostMapping("/isShow")
    @ApiOperation(value="切换是否展示")
    public AjaxResult<Object> isShow(@Validated @RequestBody IdValidate idValidate) {
        Integer adminId = LikeAdminThreadLocal.getAdminId();
        iLegalCaseService.isShow(idValidate.getId(), adminId);
        return AjaxResult.success();
    }

    @Log(title = "切换律师是否可以接单")
    @PostMapping("/canTake")
    @ApiOperation(value="切换律师是否可以接单")
    public AjaxResult<Object> canTake(@Validated @RequestBody IdValidate idValidate) {
        Integer adminId = LikeAdminThreadLocal.getAdminId();
        iLegalCaseService.canTake(idValidate.getId(), adminId);
        return AjaxResult.success();
    }

}
